/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modele;

import exceptions.DataSourceException;
import exceptions.ClientNotFoundException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.sql.DataSource;


/**
 
 * @author Jean-Michel
 */
public class ClientImpl implements InterfaceClient {

    private Properties props = new Properties();
    private DataSource ds = null;

    @Override
    public Client getClient(String username) throws DataSourceException, ClientNotFoundException {

        Connection conn = null;
        ResultSet rs = null;
        Statement stmt = null;
        Client clt;

        try {
            conn = this.ds.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery("select * from client where id="
                    + username);

            if (rs.next()) {
                clt = new Client();
                clt.setNom(rs.getString(username));
                clt.setAdresse(rs.getString(username));
                clt.setDateCrea(rs.getString(username));
                clt.setDateNaiss(rs.getString(username));
                clt.setId(Integer.getInteger(rs.getString(username)));
                clt.setNbCompte(Integer.getInteger(rs.getString(username)));
                clt.setPrenom(rs.getString(username));
            } else {
                throw new ClientNotFoundException("Client avec id " + username + " non trouve!");
            }

        } catch (SQLException e) {

            throw new DataSourceException();
        }
        return clt;
    }

    @Override
    public void createClient(Client employe) throws DataSourceException {

        Connection conn = null;
        ResultSet rs = null;
        Statement stmt = null;

        try {
            conn = this.ds.getConnection();
            stmt = conn.createStatement();
            
            StringBuffer sqlString =
                    new StringBuffer("insert into client(nom,prenom,adresse,DATENAISS,DATECREA,NBCOMPTE)");

            sqlString.append("values ('"
                    + employe.getNom() + "', ");
            sqlString.append("'"
                    + employe.getPrenom() + "', ");
            sqlString.append(
                    employe.getAdresse() + ", ");
            sqlString.append("'"
                    + employe.getDateNaiss() + "', ");
            sqlString.append("sysdate, ");
            sqlString.append(
                    0+ ")");

            stmt.execute(sqlString.toString());
        } catch (SQLException e) {

            throw new DataSourceException();
        }

    }

    @Override
    public List<Client> getAllClients() throws DataSourceException {

        List clts = null;
        Connection conn = null;
        ResultSet rs = null;
        Statement stmt = null;

        try {
            conn = this.ds.getConnection();
            stmt = conn.createStatement();

            rs = stmt.executeQuery("select * from client");

            Client clt;
            clts = new ArrayList<Client>();

            while (rs.next()) {

                clt = new Client();
                clt.setId(new Integer(rs.getString("idclient")));
                clt.setNom(rs.getString("nom"));
                clt.setPrenom(rs.getString("prenom"));
                clt.setAdresse(rs.getString("adressse"));
                clt.setDateNaiss(rs.getString("datenaiss"));
                clt.setDateCrea(rs.getString("datecrea"));
                clt.setNbCompte(new Integer(rs.getString("nbcompte")));
                clts.add(clt);
            }
        } catch (SQLException e) {
            throw new DataSourceException();
        }
        return clts;

    }

    @Override
    public void updateClient(Client employe) throws DataSourceException {

        Connection conn = null;
        ResultSet rs = null;
        Statement stmt = null;

        try {
            conn = this.ds.getConnection();
            stmt = conn.createStatement();
            StringBuffer sqlString =
                    new StringBuffer("update client ");

            sqlString.append("set nom='"
                    + employe.getNom() + "', ");
            sqlString.append("prenom='"
                    + employe.getPrenom() + "', ");
            sqlString.append("adresse='"+
                    employe.getAdresse() + "', ");
            sqlString.append("dateNaiss='"
                    + employe.getDateNaiss() + "', ");
            sqlString.append("dateCrea='"
                    + employe.getDateCrea() + 
                    "' where id ="+employe.getId());

            stmt.execute(sqlString.toString());
        } catch (SQLException e) {

            throw new DataSourceException();
        }

        
    }

    public DataSource getDataSource() throws DataSourceException {
        return ds;
    }

    public void setDataSource(DataSource ds) throws DataSourceException {
        this.ds = ds;
    }
}
